<!DOCTYPE html>
<html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <head>Test</head>
    <body>
        <form>
            <fieldset>
                    
                    <select id="country"></select>
                    <select id="currency"></select>
                    <select id="language"></select>
                    <select id="meaunit"></select>

                    <select id="plant"></select>
                    <select id="company" disabled></select>
                    <select id="pgrp" disabled></select>
                    <select id="porg" disabled></select>
                    
                    <select id="sorg"></select>
                    <select id="distrchannel">
                        <option value="">distrchannel</option>
                        <option value="IN">Internet</option>
                        <option value="WH">Wholesale</option>
                    </select>
                    <select id="tptype"></select>

            </fieldset> 
        </form>

        <textarea id="test"></textarea>

        <script>
            //plant改变
            $(document).ready(function() {
                $("#plant2").change(function(){ 
                    
                    $.ajax({
                        type: "post",
                        url: "{% url 'MM:ajax_getStockByName' %}",
                        data:{
                            csrfmiddlewaretoken: '{{ csrf_token }}',
                            //plant值
                            name:$("#plant2").val(),
                        },
                        success: function(data) {
                            $('#test').text(data);
                            //add：如果没有该工厂，回传回来一个message提示
                            //[{'message': '暂无该工厂相关数据，请重新确认！', 'fields': init}]
                            var dataObj = eval("("+data+")");
                            //注意：plant选择空的时候，这三项也返回空
                            fields = dataObj[0].fields
                            $("#pgrp2").val(fields.pGrp);
                            $("#porg2").val(fields.pOrg);
                            $("#company2").val(fields.companyCode);
                        },
                        failure: function() {
    
                        }
                    });
                
                }); 
                
            });

            //编号搜索 如果搜不到，返回提示：没有符合条件的商品（这个就不返回了，可以判断是否为空）
            $('#material2').on('submit', function(e) {
                e.preventDefault();
                $.ajax(
                {
                    type: "POST",
                    url: "{% url 'MM:ajax_search_item' %}",
                    data:{
                        dataType: "json",
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                        mid: '',
                        mname: $("#mname").val(),
                        mType: $("#mType").val(),
                        industrySector: $("#industrySector").val(),
                        sloc: $("#sloc").val(),
                        plant: $("#plant").val(),
                        uid: $('#uid').val(),
                    },
                    success: function(data) {

                        $('#item_result').text(data);
                    
                        var dataObj = eval("("+data+")");
                        var out = "";
                        var i;
                        
                        for(i = 0; i < dataObj.length; i++) {
                            //dataObj[i].fields.xx
                            fields = dataObj[i].fields
                            material = dataObj[i].material
                            stock = dataObj[i].stock
                            user = dataObj[i].user
                            out += '<tr><td>'+ material.id+ '</td><td>' + material.mname+ '</td><td>' + material.shortText+ '</td><td>' + material.mType+ '</td><td>' + material.industrySector+ '</td><td>'+ stock.name+ '</td><td>'+ fields.sloc+ '</td><td>'+ user.username + '</td><tr>';
                        }
                        document.getElementById("searchDiv").innerHTML=out;
                    },
                    failure: function() {

                    }
                })
            });

            //复制商品
            $("table").on("dblclick","tr",function(e){
                e.preventDefault();
                $.ajax(
                    {
                        type: "POST",
                        url: "{% url 'MM:ajax_search_item' %}",
                        data:{
                            dataType: "json",
                            csrfmiddlewaretoken: '{{ csrf_token }}',
                            //输入三个搜索框的值
                            mid: '',
                            mname: $(this).find('td').eq(1).text(),
                            plant: $(this).find('td').eq(5).text(),
                            sloc: $(this).find('td').eq(6).text(),
                        },
                        success: function(data) {
                            $('#item_result').text(data);
                            var dataObj = eval("("+data+")");
                            fields = dataObj[0].fields;
                            material = dataObj[0].material;
                            stock = dataObj[0].stock;
                            user = dataObj[0].user;
                            //输入表单material3的属性的id
                            //$("#pk2").val(material.id);
                            $("#mname2").val(material.mname); 
                            $("#mType2").val(material.mType);
                            $("#meaunit2").val(material.meaunit);
                            $("#netWeight2").val(material.netWeight);
                            $("#weightUnit2").val(material.weightUnit);
                            $("#transGrp2").val(material.transGrp);
                            $("#loadingGrp2").val(material.loadingGrp);
                            $("#industrySector2").val(material.industrySector);
                            $("#mGroup2").val(material.mGroup);
                            $("#sloc2").val(fields.sloc);
                            $("#sorg2").val(fields.sOrg);
                            $("#distrchannel2").val(fields.distrChannel);
                            $("#company2").val(stock.companyCode);
                            $("#porg2").val(stock.pOrg);
                            $("#pgrp2").val(stock.pGrp);
                            $("#plant2").val(stock.name);
                            $("#shortText2").val(material.shortText);
                            //document.getElementById("material3").style.visibility="visible";
                        },
                        failure: function() {
    
                        }
                    })
                $('#myModal1').modal('hide');
            });

            //提交创建信息，返回商品编号，商品编号和创建人编号进入数据库
            //0-> fail; 1->succeed; {'status':1, 'message':"xxx", 'fields':[xxx,xxx]}
            $('#material3').on('submit', function(e) {
                e.preventDefault();
                $.ajax(
                {
                    type: "POST",
                    url: "{% url 'MM:ajax_create_item' %}",
                    data:{
                        dataType: "json",
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                        //material_id: $("#pk2").val(),
                        mname: $("#mname2").val(),
                        mType: $("#mType2").val(),
                        meaunit: $("#meaunit2").val(),
                        netWeight: $("#netWeight2").val(),
                        weightUnit: $("#weightUnit2").val(),
                        transGrp: $("#transGrp2").val(),
                        loadingGrp: $("#loadingGrp2").val(),
                        industrySector: $("#industrySector2").val(),
                        mGroup: $("#mGroup2").val(),
                        sloc: $("#sloc2").val(),
                        sOrg: $("#sorg2").val(),
                        distrChannel: $("#distrchannel2").val(),
                        companyCode: $("#company2").val(),
                        pOrg: $("#porg2").val(),
                        pGrp: $("#pgrp2").val(),
                        name: $("#plant2").val(),
                        shortText: $("#shortText2").val(),
                        //输入修改信息
                    },
                    success: function(data) {
                        $('#item_result').text(data);
                        var dataObj = eval("("+data+")");
                        //0-> fail; 1->succeed; {'status':1, 'message':"xxx", 'fields':[xxx,xxx]}
                        //根据status写。。。
                    },
                    failure: function() {

                    }
                })
            });

            $(document).ready(function () {
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_country' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >country</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Country+'>'+ dataObj[i].ChineseName + '--' + dataObj[i].EnglishName + '</option>' ;
                        }
                        document.getElementById("country").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_company' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >company</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].CompanyCode+'>'+ dataObj[i].CompanyCode + '--' + dataObj[i].Name + '</option>' ;
                        }
                        document.getElementById("company").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_currency' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >currency</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Currency+'>'+ dataObj[i].Currency + '--' + dataObj[i].Name + '</option>' ;
                        }
                        document.getElementById("currency").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_currency' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >请选择交易货币</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Currency+'>'+ dataObj[i].Currency + '--' + dataObj[i].Name + '</option>' ;
                        }
                        var items=document.getElementsByName("currency");
                        for(i = 0; i < items.length; i++) {
                            items[i].innerHTML=out;
                        }
                    },
                    failure: function() {
    
                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_language' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >language</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Language+'>'+ dataObj[i].Language + '--' + dataObj[i].Name + '</option>' ;
                        }
                        document.getElementById("language").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_meaunit' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >meaunit</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Meaunit+'>'+ dataObj[i].Meaunit + '--' + dataObj[i].Name + '</option>' ;
                        }
                        document.getElementById("meaunit").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_pgrp' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >pgrp</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Pgrp+'>'+ dataObj[i].Pgrp + '--' + dataObj[i].Name + '</option>' ;
                        }
                        document.getElementById("pgrp").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_plant' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >plant</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Plant+'>'+ dataObj[i].Plant + '--' + dataObj[i].Name + '</option>' ;
                        }
                        document.getElementById("plant").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_porg' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >porg</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Porg+'>'+ dataObj[i].Porg + '--' + dataObj[i].Name + '</option>' ;
                        }
                        document.getElementById("porg").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_sorg' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >sorg</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Sorg+'>'+ dataObj[i].Sorg + '--' + dataObj[i].Name1 + '</option>' ;
                        }
                        document.getElementById("sorg").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
                $.ajax({
                    type: "post",
                    url: "{% url 'MM:ajax_load_tptype' %}",
                    data:{
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                    },
                    success: function(data) {
                        var dataObj = eval("("+data+")");
                        var out = "<option value='' >company</option>";
                        var i;
                        for(i = 0; i < dataObj.length; i++) {
                            out += '<option value=' + dataObj[i].Tptype+'>'+ dataObj[i].Tptype + '--' + dataObj[i].Description + '</option>' ;
                        }
                        document.getElementById("tptype").innerHTML=out;
                    },
                    failure: function() {

                    }
                });
            });
        </script>

    </body>
</html>